﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Raovat_ThongSoKyThuat
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            LoadType()
            If Not Request.QueryString("RaovatId") Is Nothing Then
                hidID.Value = Request.QueryString("RaovatId")
                ShowData()
            End If
        End If


    End Sub
    Protected Sub LoadType()
        Using Data As New VSEntities

            'Dong co
            ddlDongCo.AppendDataBoundItems = True
            ddlDongCo.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim t = (From k In Data.vDongCoes Select k).ToList
            ddlDongCo.DataTextField = "NAME"
            ddlDongCo.DataValueField = "ID"
            ddlDongCo.DataSource = t
            ddlDongCo.DataBind()

            'Kieu Dong co
            ddlKieuDongCo.AppendDataBoundItems = True
            ddlKieuDongCo.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim n = (From h In Data.vKieuDongCoes Select h).ToList
            ddlKieuDongCo.DataTextField = "NAME"
            ddlKieuDongCo.DataValueField = "ID"
            ddlKieuDongCo.DataSource = n
            ddlKieuDongCo.DataBind()

            'Phanh
            ddlPhanh.AppendDataBoundItems = True
            ddlPhanh.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim u = (From h In Data.vPhanhs Select h).ToList
            ddlPhanh.DataTextField = "NAME"
            ddlPhanh.DataValueField = "ID"
            ddlPhanh.DataSource = u
            ddlPhanh.DataBind()

            'Giam soc
            ddlGiamSoc.AppendDataBoundItems = True
            ddlGiamSoc.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim l = (From r In Data.vPhanhs Select r).ToList
            ddlGiamSoc.DataTextField = "NAME"
            ddlGiamSoc.DataValueField = "ID"
            ddlGiamSoc.DataSource = l
            ddlGiamSoc.DataBind()

            'Vanh mam
            ddlVanhMam.AppendDataBoundItems = True
            ddlVanhMam.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim g = (From h In Data.vVanhMams Select h).ToList
            ddlVanhMam.DataTextField = "NAME"
            ddlVanhMam.DataValueField = "ID"
            ddlVanhMam.DataSource = g
            ddlVanhMam.DataBind()
            'Lop xe
            ddlLopXe.AppendDataBoundItems = True
            ddlLopXe.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim z = (From x In Data.vLopXes Select x).ToList
            ddlLopXe.DataTextField = "NAME"
            ddlLopXe.DataValueField = "ID"
            ddlLopXe.DataSource = z
            ddlLopXe.DataBind()
        End Using
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As ThongSoKyThuat = (From q In data.ThongSoKyThuats Where q.RaoVatId = hidRaoVatId.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                txtDaiRongCao.Text = IIf(IsNothing(p.DaiRongCao) = True, "", p.DaiRongCao)
                txtChieudaicoso.Text = IIf(IsNothing(p.ChieuDaiCoSo) = True, "", p.ChieuDaiCoSo)
                txtChieurongcoso.Text = IIf(IsNothing(p.ChieuRongCoSo) = True, "", p.ChieuRongCoSo)
                txtTrongluongkhongtai.Text = IIf(IsNothing(p.TrongLuongKhongTai) = True, "", p.TrongLuongKhongTai)
                txtDungtichbinhnhienlieu.Text = IIf(IsNothing(p.DungTichBinhNhienLieu) = True, "", p.DungTichBinhNhienLieu)
                ddlDongCo.SelectedValue = IIf(IsNothing(p.DongCo) = True, 0, p.DongCo)
                ddlKieuDongCo.SelectedValue = IIf(IsNothing(p.KieuDongCo) = True, 0, p.KieuDongCo)
                txtDungtichxilanh.Text = IIf(IsNothing(p.DungTichXiLanh) = True, "", p.DungTichXiLanh)
                ddlPhanh.SelectedValue = IIf(IsNothing(p.Phanh) = True, 0, p.Phanh)
                ddlGiamSoc.SelectedValue = IIf(IsNothing(p.GiamSoc) = True, 0, p.GiamSoc)
                ddlLopXe.SelectedValue = IIf(IsNothing(p.LopXe) = True, 0, p.LopXe)
                ddlVanhMam.SelectedValue = IIf(IsNothing(p.VanhMam) = True, 0, p.VanhMam)
                txtDescription.Text = IIf(IsNothing(p.ThongSoKhac) = True, "", p.ThongSoKhac)

            End If
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            If hidRaoVatId.Value <> "0" Then
                Dim p As ThongSoKyThuat = (From q In data.ThongSoKyThuats Where q.RaoVatId = hidRaoVatId.Value Select q).FirstOrDefault
                Try
                    p.DaiRongCao = txtDaiRongCao.Text.Trim()
                    p.ChieuDaiCoSo = txtChieudaicoso.Text.Trim()
                    p.ChieuRongCoSo = txtChieurongcoso.Text.Trim()
                    p.TrongLuongKhongTai = txtTrongluongkhongtai.Text.Trim()
                    p.DungTichBinhNhienLieu = txtDungtichbinhnhienlieu.Text.Trim()
                    p.DongCo = ddlDongCo.SelectedValue
                    p.KieuDongCo = ddlKieuDongCo.SelectedValue
                    p.DungTichXiLanh = txtDungtichxilanh.Text.Trim()
                    p.Phanh = ddlPhanh.SelectedValue
                    p.GiamSoc = ddlGiamSoc.SelectedValue
                    p.LopXe = ddlLopXe.SelectedValue
                    p.VanhMam = ddlVanhMam.SelectedValue
                    p.ThongSoKhac = txtDescription.Text.Trim()
                    p.RaoVatId = hidRaoVatId.Value

                    data.SaveChanges()
                    'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/List.aspx';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            Else
                Dim p As New VSModel.ThongSoKyThuat
                Try
                    p.DaiRongCao = txtDaiRongCao.Text.Trim()
                    p.ChieuDaiCoSo = txtChieudaicoso.Text.Trim()
                    p.ChieuRongCoSo = txtChieurongcoso.Text.Trim()
                    p.TrongLuongKhongTai = txtTrongluongkhongtai.Text.Trim()
                    p.DungTichBinhNhienLieu = txtDungtichbinhnhienlieu.Text.Trim()
                    p.DongCo = ddlDongCo.SelectedValue
                    p.KieuDongCo = ddlKieuDongCo.SelectedValue
                    p.DungTichXiLanh = txtDungtichxilanh.Text.Trim()
                    p.Phanh = ddlPhanh.SelectedValue
                    p.GiamSoc = ddlGiamSoc.SelectedValue
                    p.LopXe = ddlLopXe.SelectedValue
                    p.VanhMam = ddlVanhMam.SelectedValue
                    p.ThongSoKhac = txtDescription.Text.Trim()
                    p.RaoVatId = hidRaoVatId.Value
                    data.ThongSoKyThuats.AddObject(p)
                    data.SaveChanges()
                    'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Raovat/List.aspx';", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                End Try
            End If

        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
